//
//  CityChooseView.m
//  kdxshd
//
//  Created by admin on 16/5/11.
//  Copyright © 2016年 zjw. All rights reserved.
//

#import "CityChooseView.h"
@implementation CityChooseView
@synthesize mywindow;
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    [self showcitychooseview];
    
    return self;
}


- (void)showcitychooseview
{
    mywindow = [[UIWindow alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height)];
    mywindow.backgroundColor = mRGBACOLOR(0, 0, 0, 0.5);
    mywindow.windowLevel = UIWindowLevelAlert;
    [mywindow makeKeyAndVisible];
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handTap:)];
    [mywindow addGestureRecognizer:tap];
    
    _mypiker=[[UIPickerView alloc]initWithFrame:CGRectMake(12, Main_Screen_Height/2-180/2,Main_Screen_Width-2*12, 220)];
    [_mypiker setBackgroundColor:mRGBColor(245, 245, 245)];
    _mypiker.delegate=self;
    _mypiker.dataSource=self;
    _mypiker.showsSelectionIndicator=YES;
    [CZNsjHelper SetTopcornerRadius:_mypiker];
    [mywindow addSubview:_mypiker];
    
    UIButton* okBtn = [[UIButton alloc]initWithFrame:CGRectMake(12, _mypiker.bottom, Main_Screen_Width-2*12, 40)];
    [okBtn setBackgroundColor:ThemeColor];
    [okBtn setTitle:@"确定" forState:UIControlStateNormal];
    [okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    okBtn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [CZNsjHelper SetBottomcornerRadius:okBtn];
    [okBtn addTarget:self action:@selector(onclickOkBtn) forControlEvents:UIControlEventTouchUpInside];
    [mywindow addSubview:okBtn];
    
    cityrecordarray = [[NSMutableArray alloc]init];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"City" ofType:@"plist"];//找到该plist的路径
    // NSLog(@"%@",path);
    NSDictionary* myDotaDictionary = [[NSDictionary alloc]initWithContentsOfFile:path];
    
    NSArray* array = [[NSArray alloc]init];
    array = [myDotaDictionary objectForKey:@"address"];
    
    ProviceArray = [[NSMutableArray alloc]init];
    CityArray = [[NSMutableArray alloc]init];
    SubArray = [[NSMutableArray alloc]init];
    
    for (NSDictionary* dict in array)
    {
        [ProviceArray addObject:[dict objectForKey:@"name"]];
        
        NSString* zhixiashi = [dict objectForKey:@"zhixiashi"];
        //直辖市
        if ([zhixiashi isEqualToString:@"1"])
        {
            [cityrecordarray addObject:@"1"];
            
            NSArray* array1 = [dict objectForKey:@"sub"];
            NSMutableArray* mycityarray2 = [[NSMutableArray alloc]init];
            for (NSDictionary* mydict in array1)
            {
                [mycityarray2 addObject:[mydict objectForKey:@"name"]];
                [CityArray addObject:mycityarray2];
                NSArray* array2 = [mydict objectForKey:@"sub"];
                NSMutableArray* myarray = [[NSMutableArray alloc]init];
                for (NSString* substring1 in array2)
                {
                    [myarray addObject:substring1];
                }
                [SubArray addObject:myarray];
            }
        }
        //普通省份
        else if ([zhixiashi isEqualToString:@"0"])
        {
            [cityrecordarray addObject:@"0"];
            
            NSArray* array2 = [dict objectForKey:@"sub"];
            NSMutableArray* mycityarray = [[NSMutableArray alloc]init];
            NSMutableArray* allsubarray = [[NSMutableArray alloc]init];
            for (NSDictionary* citydict in array2)
            {
                NSString* cityname = [citydict objectForKey:@"name"];
                [mycityarray addObject:cityname];
                
                NSArray* mysubarray = [citydict objectForKey:@"sub"];
                NSMutableArray* mysubarray1 = [[NSMutableArray alloc]init];
                for (NSString* sub in mysubarray)
                {
                    [mysubarray1 addObject:sub];
                }
                [allsubarray addObject:mysubarray1];
            }
            [CityArray addObject:mycityarray];
            [SubArray addObject:allsubarray];
        }
        //特别行政区
        else if ([zhixiashi isEqualToString:@"2"])
        {
            [cityrecordarray addObject:@"1"];
            
            NSArray* cityname1 = [dict objectForKey:@"sub"];
            for (NSDictionary* mydict1 in cityname1)
            {
                [CityArray addObject:[mydict1 objectForKey:@"name"]];
            }
        }
    }
    
    showcityarray = [CityArray[0] mutableCopy];
    
    showsubarray = [SubArray[0] mutableCopy];
    
    [_mypiker reloadAllComponents];
    
}


#pragma mark - UIPickerDelegate && UIPickDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0)
    {
        return [ProviceArray count];
    }
    else if (component == 1)
    {
        return [showcityarray count];
    }
    else if (component == 2)
    {
        return [showsubarray count];
    }
    return 0;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 44;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component==0)
    {
        return [ProviceArray objectAtIndex:row];
    }
    else if (component == 1)
    {
        return [showcityarray objectAtIndex:row];
    }
    else if (component == 2)
    {
        return [showsubarray objectAtIndex:row];
    }
    return nil;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0)
    {
        if (row > [ProviceArray count]-1)
        {
            return;
        }
        showcityarray = [[NSMutableArray alloc]init];
        showcityarray = [CityArray[row] mutableCopy];
        
        //直辖市
        if ([cityrecordarray[row] isEqualToString:@"1"])
        {
            showsubarray = [[NSMutableArray alloc]init];
            showsubarray = [SubArray[row] mutableCopy];
            
            selectrow1 = 0;
        }
        //普通省份
        else if ([cityrecordarray[row] isEqualToString:@"0"])
        {
            showsubarray = [[NSMutableArray alloc]init];
            
            if (selectrow1 > ([SubArray[row] count]))
            {
                showsubarray = [SubArray[row][[SubArray[row] count]-1] mutableCopy];
            }
            else
            {
                showsubarray = [SubArray[row][selectrow1] mutableCopy];
            }
        }
        selectrow = row;
        
        [_mypiker reloadComponent:1];
        [_mypiker reloadComponent:2];
        
    }
    else if (component == 1)
    {
        //直辖市
        if ([cityrecordarray[selectrow] isEqualToString:@"1"])
        {
            showsubarray = [[NSMutableArray alloc]init];
            showsubarray = [SubArray[row] mutableCopy];
        }
        //普通省份
        else if ([cityrecordarray[selectrow] isEqualToString:@"0"])
        {
            showsubarray = [[NSMutableArray alloc]init];
            showsubarray = [SubArray[selectrow][row] mutableCopy];
        }
        
        selectrow1 = row;
        
        [_mypiker reloadComponent:2];
        
    }
    else if (component == 2)
    {
        selectrow2 = row;
    }
}


// 设置字体大小
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.minimumScaleFactor = 8.0;
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}


- (void)onclickOkBtn
{
    
    mywindow.hidden = YES;
    mywindow = nil;
    
    NSInteger selectedProvinceIndex = [_mypiker selectedRowInComponent:0];
    NSInteger selectedCityIndex = [_mypiker selectedRowInComponent:1];
    NSInteger selectedSubIndex = [_mypiker selectedRowInComponent:2];
    
    provicestring = [ProviceArray objectAtIndex:selectedProvinceIndex];
    citystring = [CityArray[selectedProvinceIndex] objectAtIndex:selectedCityIndex];
    if ([cityrecordarray[selectedProvinceIndex] isEqualToString:@"1"])
    {
        substring = [SubArray[selectedCityIndex] objectAtIndex:selectedSubIndex];
    }
    else
    {
        substring = [SubArray[selectedProvinceIndex][selectedCityIndex] objectAtIndex:selectedSubIndex];
    }
    
    if (self.SelectCityOk)
    {
        self.SelectCityOk(provicestring,citystring,substring);
    }
    [self removeFromSuperview];
}

- (void)handTap:(UITapGestureRecognizer*)tap
{
    
    [self removeFromSuperview];
    
    mywindow.hidden = YES;
    mywindow = nil;
}
@end
